package Test;

public class 买卖股票 {
    public int maxProfit(int[] prices) {
        int[][] dp = new int[prices.length][2];
        for (int i = 0; i < prices.length; i++) {
            if (i == 0) {
                dp[i][0] = 0;
                dp[i][1] = -prices[i];
                continue;
            }
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            //不能在同一天出售的话是这种，因为限制了交易次数,k为0的情况下，买股票只有减少钱
            // dp[i][1]=Math.max(dp[i-1][1],-prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[prices.length - 1][0];
    }


    //第三种
    public int maxProfit3(int[] prices) {
       int[][][] dp=new int[prices.length][3][2];
       for (int i=1;i<=2;i++){
           for(int j=0;j<prices.length;j++){
               if (i == 0) {
                   dp[j][i][0] = 0;
                   dp[j][i][1] = -prices[i];
                   continue;
               }
               dp[j][i][0]=Math.max(dp[j-1][i][0],dp[j-1][i][1]+prices[i]);
               dp[j][i][1]=Math.max(dp[j-1][i][1],dp[j-1][i-1][0]-prices[j]);
           }
       }
       return dp[prices.length-1][2][0];
    }
}